/*
 * SAM4E_SAM_BA_Monitor_software_call.c
 *
 * Created: 11/7/2014 11:28:35 AM
 *  Author: jerome.semette
 */ 

#include "sam.h"
#define RAMFUNC __attribute__ ((long_call, section (".ramfunc")))


RAMFUNC void SAMBA_reboot (void);

RAMFUNC void SAMBA_reboot (void){
	
	/* Wait until Flash is ready */
	while(!(EFC0->EEFC_FSR & EEFC_FSR_FRDY));
	
	/* Send Clear GPNVM bit command with argument 1 (Clear GPNVM1) */
	EFC0->EEFC_FCR = (EEFC_FCR_FKEY_PASSWD | EEFC_FCR_FCMD_CGPB |(1 << EEFC_FCR_FARG_Pos));
	while(!(EFC0->EEFC_FSR & EEFC_FSR_FRDY));
	
	/* Perform Software reset */
	RSTC->RSTC_CR |= (RSTC_CR_KEY_PASSWD | RSTC_CR_PROCRST | RSTC_CR_PERRST);
	
	/* Infinite loop to avoid further code execution - chip should reset */
	while(1);	
}


/**
 * \brief Application entry point.
 *
 * \return Unused (ANSI-C compatibility).
 */
int main(void)
{

    /* Initialize the SAM system */
    SystemInit();
	
	/* Call SAM-BA reboot function */ 
	SAMBA_reboot();
	
	/* Main infinite loop (should not be reached) */
    while (1) ;	
}
